﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.ColorSpaces
{
    /// <summary>
    /// Class to convert RGB to YUV and back
    /// </summary>
    public class YUV444 : YUV
    {
        /// <summary>
        /// Primary constructor
        /// </summary>
        /// <param name="_color"></param>
        public YUV444(Color _color)
            : base(_color)
        {

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="YUV444"/> class.
        /// </summary>
        /// <param name="_Y">The _ Y.</param>
        /// <param name="_U">The _ U.</param>
        /// <param name="_V">The _ V.</param>
        public YUV444(float _Y, float _U, float _V)
            : base(_Y, _U, _V)
        {
        }

        /// <summary>
        /// used by clone to create a deep copy
        /// </summary>
        /// <param name="_yuv444"></param>
        internal YUV444(YUV444 _yuv444)
            : base(_yuv444)
        {
        }

        /// <summary>
        /// NTSC
        /// </summary>
        protected override void CalcValues()
        {
            m_Y = 0.299f * m_Color.R + 0.587f * m_Color.G + 0.114f * m_Color.B;
            m_U = -0.147f * m_Color.R - 0.289f * m_Color.G + 0.436f * m_Color.B;
            m_V = 0.615f * m_Color.R - 0.515f * m_Color.G - 0.100f * m_Color.B;
        }

        /// <summary>
        /// retransform back to rgb color space
        /// </summary>
        /// <returns></returns>
        public override Color ToColor()
        {
            float fR = 1.0f * m_Y + 0.0f * m_U + 1.140f * m_V;
            float fG = 1.0f * m_Y - 0.394f * m_U - 0.581f * m_V;
            float fB = 1.0f * m_Y + 2.028f * m_U + 0.0f * m_V;
            
            if (fR < 0) fR = 0; if (fR > 255) fR = 255;
            if (fG < 0) fG = 0; if (fG > 255) fG = 255;
            if (fB < 0) fB = 0; if (fB > 255) fB = 255;
            return Color.FromArgb((int)fR, (int)fG, (int)fB);
        }

        /// <summary>
        /// used to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new YUV(this);
        }
    }
}
